package com.eattravel.service.impl;

import com.eattravel.mapper.UserMapper;
import com.eattravel.md5.MD5Demo;
import com.eattravel.pojo.User;
import com.eattravel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userinfoMapper;

    @Override
    public List<User> findAllUser() {
        

        List<User> users=userinfoMapper.findAllUser();

        return users;
    }

    @Override
    public User login(String username, String pwd) {

      User userinfo=userinfoMapper.login(username,MD5Demo.md5(pwd));

        if(null != userinfo ) {

            return userinfo;
        }

        return null;
    }

    /**
     * 使用加密的登录验证方式
     * @param username
     * @param pwd
     * @return
     */
    public User login2(String username, String pwd) {
        String pwds = MD5Demo.md5(pwd);
        User userinfo=userinfoMapper.login(username,pwds);
        if (null != userinfo){
            return userinfo;
        }

        return null;
    }

        /**
         * 注册登录未使用加密
         * @param user
         * @return
         * @author zx
         */
    @Override
    public boolean regist(User user) {

        User user1=userinfoMapper.findByUsername(user.getUserId());

        if(user1 !=null){
            //用户名存在

            return false;
        }
        // 获取未加密的密码
        String pwd = user.getUserPassword();
        // 设置加密后的密码
        user.setUserPassword(MD5Demo.md5(pwd));

        userinfoMapper.saveUser(user);
        return true;

    }

    /**
     * 加密的注册登录模块
     * @param user
     * @return
     * @author gbl
     */
    @Override
    public boolean regist2(User user) {
        User user1=userinfoMapper.findByUsername(user.getUserId());
        // 如果用户存在，注册失败
        if(user1 != null){
            return false;
        }
        // 获取未加密的密码
        String pwd = user.getUserPassword();
        // 设置加密后的密码
        user.setUserPassword(MD5Demo.md5(pwd));
        // 存储加密后的密码
        userinfoMapper.saveUser(user);
        return true;
    }

    @Override
    public User findUserByUserId(String userId) {
        User user =userinfoMapper.findByUsername(userId);
        return user ;
    }

    @Override
    public int updateUserInfo(User user) {
        int count=userinfoMapper.updateUserInfo(user);

        return count;
    }


}
